package jl.cts.util;

import java.util.Vector;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;

/**
 * A sprite class adapted to manage frames and set automatically animations.
 * This class manages animation, transition and rotation of an object.
 * 
 * @author NorthSouth Studios
 * 
 */
public class AnimatedSprite extends Sprite {

	public boolean isInfinite, stop, move;
	private Vector<Integer> imageIdList = null;
	private int totalImg, currentImage, startBmp;

	private int numReps = 1;

	/**
	 * Animated Sprite constructor
	 * 
	 * @param x
	 * @param y
	 * @param start
	 * @param bitmapList
	 */
	public AnimatedSprite(Context context, int x, int y, int start,
			Vector<Integer> bitmapList) {
		super(context, bitmapList.get(start), x, y);
		this.imageIdList = bitmapList;
		this.setTotalImg(bitmapList.size());
		this.currentImage = start;
		this.startBmp = start;
		this.isInfinite = false;
		this.move = true;
		this.stop = false;
		this.totalImg = bitmapList.size();
	}

	@Override
	public void render(Canvas canvas, Paint paint) {
		super.render(canvas, paint);

		if (numReps > 0 && !stop) {

			if (move) {
				currentImage++;
				if (currentImage >= this.totalImg - 1)
					currentImage = 0;

				if (!stop) {
					if (imageIdList != null && imageIdList.size() > 0
							&& currentImage < imageIdList.size()
							&& imageIdList.get(currentImage) != null) {
						this.setImage(imageIdList.get(currentImage));

					}
				}

				if (!isInfinite) {
					if (currentImage == startBmp) {
						stop = true;
						move = false;
						numReps--;
						if (numReps > 0) {
							resetAnimation();
						} else {

						}
					}
				}
			}
		}
	}

	/**
	 * Reset the animation
	 */
	public void resetAnimation() {
		currentImage = 0;
		startBmp = 0;
		move = true;
		stop = false;
	}

	public void unloadResources() {
		if (imageIdList != null) {
			imageIdList.removeAllElements();
			imageIdList = null;
		}
	}

	public boolean isInfinite() {
		return isInfinite;
	}

	public void setInfinite(boolean isInfinite) {
		this.isInfinite = isInfinite;
	}

	public int getTotalImg() {
		return totalImg;
	}

	public void setTotalImg(int totalImg) {
		this.totalImg = totalImg;
	}

	public boolean isMove() {
		return move;
	}

	public void setMove(boolean move) {
		this.move = move;
	}

	public boolean isStop() {
		return stop;
	}

	public void setStop(boolean stop) {
		this.stop = stop;
	}

	public Vector<Integer> getIimageIdList() {
		return imageIdList;
	}

	public void setImageIdList(Vector<Integer> imageIdList) {
		this.imageIdList = imageIdList;
	}

	public int getNumReps() {
		return numReps;
	}

	public void setNumReps(int numReps) {
		this.numReps = numReps;
		resetAnimation();
	}

	public void setCurrentImage(int currentImage) {
		if (imageIdList != null && currentImage < imageIdList.size()) {
			this.setImage(imageIdList.get(currentImage));
			this.currentImage = currentImage;
		}
	}

}